/*
 * @Descripttion:
 * @Author: liuyu
 * @Date: 2023-12-04 15:44:23
 * @LastEditors: liuyu
 * @LastEditTime: 2023-12-04 15:44:23
 * @FilePath: /app/service/role.js
 */
'use strict';
const Service = require('egg').Service;
const moment = require('moment');
class ShopProductService extends Service {
  // 获取详情信息
  async show(where) {
    const { app } = this;
    const res = await app.mysql.get('db2').get('ddwx_shop_fuwu', where);
    return res;
  }
  // 获取服务列表
  async index(columns = [], where = {}, page = '', page_size = '', field, order) {

    const sql = this.ctx.helper.createSelectSql('ddwx_shop_product', {
      columns,
      where,
      page,
      page_size,
      field,
      order,
    });
    console.log(sql);
    const res = await this.app.mysql.get('db2').query(sql);
    const sql2 = this.ctx.helper.createSelectSql('ddwx_shop_product', {
      columns,
      where,
      field,
      order,
    });
    const count = await this.app.mysql.get('db2').query(sql2);
    return {
      res,
      count,
    };
    // let sql = 'SELECT * FROM '
    // $page = input('param.page');
    // 	$limit = input('param.limit');
    // 	if(input('param.field') && input('param.order')){
    // 		$order = input('param.field').' '.input('param.order');
    // 	}else{
    // 		$order = 'sort desc,id desc';
    // 	}
    // 	$where = array();
    // 	$where[] = ['aid','=',aid];
    // 	if(bid==0){
    // 		if(input('param.bid')){
    // 			$where[] = ['bid','=',input('param.bid')];
    // 		}elseif(input('param.showtype')==2){
    // 			$where[] = ['bid','>',0];
    // 			$where[] = ['linkid','=',0];
    // 		}elseif(input('param.showtype')==21){
    // 			$where[] = ['bid','=',-1];
    // 		}else{
    // 			$where[] = ['bid','=',0];
    // 		}
    // 	}else{
    // 		$where[] = ['bid','=',bid];
    // 	}
    // 	$where[] = ['douyin_product_id','=',''];
    // 	if(input('?param.ischecked') && input('param.ischecked')!=='') $where[] = ['ischecked','=',$_GET['ischecked']];
    // 	if(input('param.name')) $where[] = ['name','like','%'.$_GET['name'].'%'];
    // 	if(input('?param.status') && input('param.status')!==''){
    // 		$status = input('param.status');
    // 		$nowtime = time();
    // 		$nowhm = date('H:i');
    // 		if($status==1){
    // 			$where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");
    // 		}else{
    // 			$where[] = Db::raw("`status`=0 or (`status`=2 and (unix_timestamp(start_time)>$nowtime or unix_timestamp(end_time)<$nowtime)) or (`status`=3 and ((start_hours<end_hours and (start_hours>'$nowhm' or end_hours<'$nowhm')) or (start_hours>=end_hours and (start_hours>'$nowhm' and end_hours<'$nowhm'))) )");
    // 		}
    // 	}
    //         if(input('?param.cid') && input('param.cid')!==''){
    // 		$cid = input('param.cid');
    // 		//子分类
    // 		$clist = Db::name('shop_category')->where('aid',aid)->where('pid',$cid)->column('id');
    // 		if($clist){
    // 			$clist2 = Db::name('shop_category')->where('aid',aid)->where('pid','in',$clist)->column('id');
    // 			$cCate = array_merge($clist, $clist2, [$cid]);
    // 			if($cCate){
    // 				$whereCid = [];
    // 				foreach($cCate as $k => $c2){
    // 					$whereCid[] = "find_in_set({$c2},cid)";
    // 				}
    // 				$where[] = Db::raw(implode(' or ',$whereCid));
    // 			}
    // 		} else {
    // 			$where[] = Db::raw("find_in_set(".$cid.",cid)");
    // 		}
    // 	}
    //         if(input('?param.cid2') && input('param.cid2')!==''){
    // 		$cid = input('param.cid2');
    // 		//子分类
    // 		$clist = Db::name('shop_category2')->where('aid',aid)->where('pid',$cid)->column('id');
    // 		if($clist){
    // 			$clist2 = Db::name('shop_category2')->where('aid',aid)->where('pid','in',$clist)->column('id');
    // 			$cCate = array_merge($clist, $clist2, [$cid]);
    // 			if($cCate){
    // 				$whereCid = [];
    // 				foreach($cCate as $k => $c2){
    // 					$whereCid[] = "find_in_set({$c2},cid2)";
    // 				}
    // 				$where[] = Db::raw(implode(' or ',$whereCid));
    // 			}
    // 		} else {
    // 			$where[] = Db::raw("find_in_set(".$cid.",cid2)");
    // 		}
    // 	}
    // 	if(input('?param.gid') && input('param.gid')!=='') $where[] = Db::raw("find_in_set(".input('param.gid/d').",gid)");

    // 	if(input('?param.wxvideo_status') && input('param.wxvideo_status')!==''){
    // 		if(input('param.wxvideo_status') < 5){
    // 			if(input('param.wxvideo_status') == 0){
    // 				$where[] = ['wxvideo_product_id','=',''];
    // 			}else{
    // 				$where[] = ['wxvideo_edit_status','=',input('param.wxvideo_status')];
    // 			}
    // 		}else{
    // 			$where[] = ['wxvideo_status','=',input('param.wxvideo_status')];
    // 		}
    // 	}
    // 	$count = 0 + Db::name('shop_product')->where($where)->count();
    // 	$data = Db::name('shop_product')->where($where)->page($page,$limit)->order($order)->select()->toArray();

    // 	$cdata = Db::name('shop_category')->where('aid',aid)->column('name','id');
    // 	if(bid > 0){
    // 		$cdata2 = Db::name('shop_category2')->Field('id,name')->where('aid',aid)->where('bid',bid)->order('sort desc,id')->column('name','id');
    // 	}
    //         $iscustomoption = 0;
    // 	foreach($data as $k=>$v){
    // 		$gglist = Db::name('shop_guige')->where('aid',aid)->where('proid',$v['id'])->select()->toArray();
    // 		$ggdata = array();
    // 		foreach($gglist as $gg){
    // 			$ggdata[] = $gg['name'].' × '.$gg['stock'] .' <button class="layui-btn layui-btn-xs layui-btn-disabled" style="color:#333">￥'.$gg['sell_price'].'</button>';
    // 		}
    //             $v['cid'] = explode(',',$v['cid']);
    //             $data[$k]['cname'] = null;
    //             if ($v['cid']) {
    //                 foreach ($v['cid'] as $cid) {
    //                     if($data[$k]['cname'])
    //                         $data[$k]['cname'] .= ' ' . $cdata[$cid];
    //                     else
    //                         $data[$k]['cname'] .= $cdata[$cid];
    //                 }
    //             }
    // 		if($v['bid'] > 0){
    // 			$v['cid2'] = explode(',',$v['cid2']);
    // 			$data[$k]['cname2'] = null;
    // 			if ($v['cid2']) {
    // 				foreach ($v['cid2'] as $cid) {
    // 					if($data[$k]['cname2'])
    // 						$data[$k]['cname2'] .= ' ' . $cdata2[$cid];
    // 					else
    // 						$data[$k]['cname2'] .= $cdata2[$cid];
    // 				}
    // 			}
    // 			$data[$k]['bname'] = Db::name('business')->where('aid',aid)->where('id',$v['bid'])->value('name');
    // 		}else{
    // 			$data[$k]['cname2'] = '';
    // 			$data[$k]['bname'] = '平台自营';
    // 		}
    // 		$data[$k]['ggdata'] = implode('<br>',$ggdata);
    // 		$data[$k]['realsalenum'] = Db::name('shop_order_goods')->where('aid',aid)->where('proid',$v['id'])->where('status','in','1,2,3')->sum('num');
    // 		if($v['status']==2){ //设置上架时间
    // 			if(strtotime($v['start_time']) <= time() && strtotime($v['end_time']) >= time()){
    // 				$data[$k]['status'] = 1;
    // 			}else{
    // 				$data[$k]['status'] = 0;
    // 			}
    // 		}
    // 		if($v['status']==3){ //设置上架周期
    // 			$start_time = strtotime(date('Y-m-d '.$v['start_hours']));
    // 			$end_time = strtotime(date('Y-m-d '.$v['end_hours']));
    // 			if(($start_time < $end_time && $start_time <= time() && $end_time >= time()) || ($start_time >= $end_time && ($start_time <= time() || $end_time >= time()))){
    // 				$data[$k]['status'] = 1;
    // 			}else{
    // 				$data[$k]['status'] = 0;
    // 			}
    // 		}
    // 		if($v['bid'] == -1) $data[$k]['sort'] = $v['sort'] - 1000000;
    //             $data[$k]['iscustomoption'] = $iscustomoption;
    // 		}
    // 	return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
    // const { app } = this;
    // const options = {
    //   columns,
    //   // auth role授权部分排序也要同步调整
    //   orders: [[ field || 'id', order || 'desc' ]],
    //   where,
    // };
    // if (page && page_size) {
    //   options.limit = page_size * 1;
    //   options.offset = (page - 1) * page_size;
    // }
    // const res = await app.mysql.get('db2').select('ddwx_shop_fuwu', options);
    // return res;
  }

  // 添加
  async create(data) {
    const { app } = this;
    data.createtime = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');

    const res = await app.mysql.get('db2').insert('ddwx_shop_fuwu', data);
    return res;
  }
  // 修改
  async update(data, where) {
    const { app } = this;
    const res = await app.mysql.get('db2').update('ddwx_shop_fuwu', data, {
      where,
    });
    return res.affectedRows === 1;
  }
  // 删除
  async delete(id) {
    const res = await this.app.mysql.get('db2').delete('ddwx_shop_fuwu', { id });
    return res;
  }
}
module.exports = ShopProductService;
